import { MongooseModule, Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { ShareDocument } from '../document/share.document';

export type UsersDocument = Users & Document;

@Schema()
export class Users extends ShareDocument {
  /**
   * 头像
   */
  @Prop({
    default: ''
  })
  avatar: string;

  /**
   * 用户名
   */
  @Prop({
    unique: true,
    required: true,
  })
  username: string;

  /**
   * 密码
   */
  @Prop({
    required: true,
  })
  password: string;

  /**
   * 姓名
   */
  @Prop({
    default: ''
  })
  name: string;

  /**
   * 邮箱
   */
  @Prop({
    default: ''
  })
  email: string;

  /**
   * 年龄
   */
  @Prop({
    min: 0,
    default: 0
  })
  age: number;

  /**
   * 电话号码
   */
  @Prop({
    default: ''
  })
  phone: string;

  // 角色类型
  @Prop({
    type: Types.ObjectId,
    default: '638455bcfdc1a266f9652204',
    ref: 'Roles'
  })
  role: Types.ObjectId;

  /**
   * 应用配置
   */
  @Prop({
    type: String,
    default: '',
  })
  appConfig: string
}

export const UsersSchema = SchemaFactory.createForClass(Users);

UsersSchema.set('toJSON', { getters: true });

export const UserMongooseModule = MongooseModule.forFeature([
  {
    name: Users.name,
    schema: UsersSchema
  },
]);
